<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区域名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域类别" prop="areaType">
        <el-select v-model="queryParams.areaType" placeholder="请选择区域类别" clearable>
          <el-option
            v-for="dict in area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleVehiclePost"
          v-hasPermi="['manage:area:add']"
        >接口</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="areaList"
      row-key="indexCode"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="区域名称" align="center" prop="name" />
      <!-- <el-table-column label="父节点内码" align="center" prop="parentIndexCode" /> -->
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="区域类别" align="center" prop="areaType">
        <template #default="scope">
          <dict-tag :options="area_type" :value="scope.row.areaType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否为货架" align="center" prop="islabel" >
        <template #default="scope">
          <el-switch
            v-model="scope.row.islabel"
            inline-prompt
            disabled
            active-color="#007bff"
            inactive-color="#ff4949"
            :active-value="1"  
            :inactive-value="0"  
            active-text="是"
            inactive-text="否"
          />
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="detail" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:area:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['manage:area:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:area:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改区域管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="areaRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="区域名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="父节点内码" prop="parentIndexCode">
          <el-tree-select
            v-model="form.parentIndexCode"
            :data="areaOptions"
            :props="{ value: 'indexCode', label: 'name', children: 'children' }"
            value-key="indexCode"
            placeholder="请选择父节点内码"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number  :min="1" v-model="form.sort" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="区域类别" prop="areaType">
          <el-select v-model="form.areaType" placeholder="请选择区域类别">
            <el-option
              v-for="dict in area_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否为货架" prop="islabel" v-if="form.indexCode != null">
          <el-radio-group v-model="form.islabel">
            <el-radio :label="1" disabled border>是</el-radio>
            <el-radio :label="0" disabled border>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="detail">
          <el-input type="textarea" style="width: 340px" maxlength="200" v-model="form.detail" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea, handleVehiclePostjs } from "@/api/manage/area";

const { proxy } = getCurrentInstance();
const { area_type } = proxy.useDict('area_type');

const areaList = ref([]);
const areaOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    name: null,
    areaType: null,
  },
  rules: {
    name: [
      { required: true, message: "区域名称不能为空", trigger: "blur" }
    ],
    parentIndexCode: [
      { required: true, message: "父节点内码不能为空", trigger: "blur" }
    ],
    areaType: [
      { required: true, message: "区域类别不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

function handleVehiclePost(){
  handleVehiclePostjs().then(response => {
    alert(response.rows)
  });
}

/** 查询区域管理列表 */
function getList() {
  loading.value = true;
  listArea(queryParams.value).then(response => {
    areaList.value = proxy.handleTree(response.data, "indexCode", "parentIndexCode");
    loading.value = false;
  });
}

/** 查询区域管理下拉树结构 */
function getTreeselect() {
  listArea().then(response => {
    areaOptions.value = [];
    const data = { indexCode: 0, name: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "indexCode", "parentIndexCode");
    areaOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    indexCode: null,
    name: null,
    parentIndexCode: null,
    sort: null,
    islabel: null,
    areaType: null,
    detail: null,
    areaPath: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("areaRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.indexCode) {
    form.value.parentIndexCode = row.indexCode;
  } else {
    form.value.parentIndexCode = 0;
  }
  open.value = true;
  title.value = "添加区域管理";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentIndexCode = row.parentIndexCode;
  }
  getArea(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改区域管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["areaRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArea(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArea(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除区域管理编号为"' + row.id + '"的数据项？').then(function() {
    return delArea(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
